//
//  DGGMobileLoginServiceViewController.m
//  DGGMobileLoginSDK
//
//  Created by dgg_hzk on 2019/12/17.
//

#import "DGGMobileLoginServiceViewController.h"
#import <WebKit/WebKit.h>
#import "DGGMobileLoginSDKPrefixHeader.h"

@interface DGGMobileLoginServiceViewController ()<WKUIDelegate, WKNavigationDelegate>

@property (nonatomic, strong) WKWebView *webView;

@end

@implementation DGGMobileLoginServiceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    config.preferences = [[WKPreferences alloc] init];
    config.preferences.javaScriptEnabled = YES;
    config.preferences.javaScriptCanOpenWindowsAutomatically = YES;
    [config.preferences setValue:@(YES) forKey:@"allowFileAccessFromFileURLs"];//关键代码
    config.processPool = [[WKProcessPool alloc] init];
    WKUserContentController *con = [[WKUserContentController alloc] init];
    config.userContentController = con;
    WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config];
    webView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:webView];
    NSURL *url = [NSURL URLWithString:self.serviceHTML];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
    self.webView = webView;
    self.webView.UIDelegate = self;
    self.webView.navigationDelegate = self;
    
    [self configNavigationBar];
}

- (void)configNavigationBar{
    self.title = self.navTitle;
    UIImage *backImage = [DGGMobileLoginConfig sharedInstance].navBackImage;
    if(!backImage){
        backImage = [UIImage imageNamed:kMobileLoginSource(@"mobileLogin_close_white.png")];
    }
    UIButton *backBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBarButton setImage:backImage forState:UIControlStateNormal];
    [backBarButton setImage:backImage forState:UIControlStateHighlighted];
    [backBarButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    view.userInteractionEnabled = YES;
    [view addSubview:backBarButton];
    backBarButton.frame = view.bounds;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
}

#pragma mark - 关闭按钮事件
- (void)backButtonAction:(UIButton *)sender{
    if(self.webView.canGoBack){
        [self.webView goBack];
    }else{
        if([self.parentViewController isKindOfClass:[UINavigationController class]] && self.navigationController.viewControllers.count > 1){
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [self dismissViewControllerAnimated:YES completion:NULL];
        }
    }
}

// 在收到响应后，决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
    NSLog(@"在收到响应后：%@",navigationResponse.response.URL.absoluteString);
    //允许跳转
    decisionHandler(WKNavigationResponsePolicyAllow);
    //不允许跳转
    //decisionHandler(WKNavigationResponsePolicyCancel);
}

// 在发送请求之前，决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
    decisionHandler(WKNavigationActionPolicyAllow);
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
